/**
 * ReportGear(2011)
 */
package com.reportgear.report.context.salary;

import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import com.reportgear.core.swing.layout.LayoutHelper;
import com.reportgear.report.context.ReportContext;
import com.reportgear.report.model.cell.value.salary.SalaryObjectType;

/**
 * 薪酬对象分组信息面板
 * 
 * @version 1.0 2011-6-13
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class SalaryObjectGroupInfoPane extends JPanel {
	/**
	 * UID
	 */
	private static final long serialVersionUID = 3293300113107553849L;
	private JComboBox cbxObject;

	private JCheckBox chk1 = new JCheckBox();
	private JCheckBox chk2 = new JCheckBox();
	private JCheckBox chk3 = new JCheckBox();
	private JCheckBox chk4 = new JCheckBox();

	private JCheckBox chkG2 = new JCheckBox();
	private JCheckBox chkG3 = new JCheckBox();

	public SalaryObjectGroupInfoPane() {
		this.setLayout(new GridBagLayout());

		this.add(new JLabel("对象层级", SwingConstants.CENTER), LayoutHelper.newGridBagConstraints(0, 0, 1, 1));
		this.add(new JLabel("是否显示", SwingConstants.CENTER), LayoutHelper.newGridBagConstraints(1, 0, 1, 1));
		this.add(new JLabel("是否分组", SwingConstants.CENTER), LayoutHelper.newGridBagConstraints(2, 0, 1, 1));

		this.add(new JLabel("层级一", SwingConstants.LEFT), LayoutHelper.newGridBagConstraints(0, 1, 1, 1));
		this.add(chk1, LayoutHelper.newGridBagConstraints(1, 1, 1, 1));

		this.add(new JLabel("层级二", SwingConstants.LEFT), LayoutHelper.newGridBagConstraints(0, 2, 1, 1));
		this.add(chk2, LayoutHelper.newGridBagConstraints(1, 2, 1, 1));
		this.add(chkG2, LayoutHelper.newGridBagConstraints(2, 2, 1, 1));

		this.add(new JLabel("层级三", SwingConstants.LEFT), LayoutHelper.newGridBagConstraints(0, 3, 1, 1));
		this.add(chk3, LayoutHelper.newGridBagConstraints(1, 3, 1, 1));
		this.add(chkG3, LayoutHelper.newGridBagConstraints(2, 3, 1, 1));

		this.add(new JLabel("层级四", SwingConstants.LEFT), LayoutHelper.newGridBagConstraints(0, 4, 1, 1));
		this.add(chk4, LayoutHelper.newGridBagConstraints(1, 4, 1, 1));

		this.add(new JLabel("成本中心(组织机构)", SwingConstants.LEFT), LayoutHelper.newGridBagConstraints(0, 5, 1, 1));

		this.cbxObject = new JComboBox(SalaryObjectType.values());
		this.add(this.cbxObject, LayoutHelper.newGridBagConstraints(1, 5, 2, 1));

		ReportContext ctx = ReportContext.INSTANCE;
		this.setGroupLevel(ctx.getGroupLevel());
		this.setShowLevel(ctx.getShowLevelList());
		this.setSalaryObjectType(ctx.getSalaryObjectType());
	}

	/**
	 * @return 薪酬对象
	 */
	public SalaryObjectType getSalaryObjectType() {
		return (SalaryObjectType) this.cbxObject.getSelectedItem();
	}

	/**
	 * 设置薪酬对象
	 * 
	 * @param salaryObjectType
	 *            薪酬对象
	 */
	public void setSalaryObjectType(SalaryObjectType salaryObjectType) {
		this.cbxObject.setSelectedItem(salaryObjectType);
	}

	/**
	 * 获取分组层级
	 * 
	 * @return 分组层级
	 */
	public int getGroupLevel() {
		if (chkG3.isSelected()) {
			return 3;
		} else if (chkG2.isSelected()) {
			return 2;
		} else {
			return 0;
		}
	}

	/**
	 * 设置分组层级
	 * 
	 * @param groupLevel
	 *            分组层级
	 */
	public void setGroupLevel(int groupLevel) {
		if (groupLevel == 2) {
			chkG2.setSelected(true);
		} else if (groupLevel == 3) {
			chkG3.setSelected(true);
		}
	}

	/**
	 * 获取显示层级
	 * 
	 * @return 显示层级
	 */
	public List<Integer> getShowLevel() {
		List<Integer> result = new ArrayList<Integer>();

		if (chk1.isSelected()) {
			result.add(1);
		}

		if (chk2.isSelected()) {
			result.add(2);
		}

		if (chk3.isSelected()) {
			result.add(3);
		}

		if (chk4.isSelected()) {
			result.add(4);
		}

		return result;
	}

	public void setShowLevel(List<Integer> levelList) {
		if (levelList.isEmpty()) {
			return;
		}

		if (levelList.contains(1)) {
			chk1.setSelected(true);
		}

		if (levelList.contains(2)) {
			chk2.setSelected(true);
		}

		if (levelList.contains(3)) {
			chk3.setSelected(true);
		}

		if (levelList.contains(4)) {
			chk4.setSelected(true);
		}
	}
}
